package com.fenghongzhang.day014;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
    private TextView tv;

    private static final String TAG = "Main2Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        tv = (TextView) findViewById(R.id.tv);

        //内部的接收者,只能用动态方式注册了.
        MyRecevier myRecevier = new MyRecevier();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.bawei.intentservice");
        registerReceiver(myRecevier,intentFilter);
    }

    public void down(View view) {
        Intent intent = new Intent(this, MyIntentService.class);
        startService(intent);
    }

    //广播接收者,内部了
    class MyRecevier extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
            String json = extras.getString("json");
            Log.i(TAG, "onReceive: "+json);

            tv.setText(json);
        }
    }
}
